local _ENV,_START = hf_setenv('new_bind_activity',true)

local skynet     = require "skynet.manager"
local constant   = require "constant"
local util       = require "util"
local utils      = require "utils"
local reason     = require "reason"
local game_def   = require "game_def"
local sharedata  = require "skynet.sharedata"
local cjson      = require "cjson"
local mail_def   = require "mail_def"

local math_random = math.random
local math_floor = math.floor
local table_insert = table.insert

local CMD = {}
function init()
    global_configs = nil
    self_act_id = nil
    self_open_state = nil
    is_temp_close = nil

    hf_set_sentinel_end(_ENV)
end

local function get_activity_time()
    return gm_time + util.get_now_time()
end

local function on_activity_start(act_end_time)
    local ok = skynet.call(".act_userdata_inter","lua","check_activity_timeout",self_act_id,act_end_time)
    if not ok then
        errlog("init_activity_data====error===")
    end
end

local function on_activity_end(is_temp_close)
    self_open_state = false
    is_temp_close = is_temp_close
    skynet.retpack(true)
end

local function check_close_activity()
   
end

local function check_week_event()
    while true do
        local ok, ret = xpcall(do_check_week_event,debug.traceback)
        if not ok then
            errlog(ret)
        end

        skynet.sleep(1000)
    end
end

function CMD.start(preload)
    self_act_id = preload.act_id
    self_open_state = true

    on_activity_start(preload.act_end_time)

    skynet.retpack(true)
end

function CMD.on_activity_end(is_force)
    on_activity_end(is_force)
end

function CMD.get_player_activity_data(uid)
    print("get_player_activity_data========bb",uid)
    local ret_data = skynet.call(".act_userdata_inter","lua","get_player_activity_data",uid,self_act_id)
    if not ret_data then
        return skynet.retpack(false)
    end

    return skynet.retpack(ret_data)
end

function CMD.add_act_play_count(uid,count)
    print("add_act_play_count=========",uid,count)
    skynet.send(".act_userdata_inter","lua","act_add_play_times",uid,self_act_id,count)
end

function CMD.act_add_login_day(uid,count)
    print("act_add_login_day=========",uid,count)
    skynet.send(".act_userdata_inter","lua","act_add_login_day",uid,self_act_id,count)
end

function CMD.act_add_day_online_time(uid,online_seconds)
    print("act_add_day_online_time=========",uid,online_seconds)
    skynet.send(".act_userdata_inter","lua","act_add_day_online_time",uid,self_act_id,online_seconds)
end

function CMD.take_task_act_award(uid,act_id,task_type,target_process,continue_time)
    print("take_task_act_award=========",uid,act_id,task_type,target_process,continue_time)
    local ok = skynet.call(".act_userdata_inter","lua","take_task_act_award",uid,act_id,task_type,target_process,continue_time)
    if not ok then
        return skynet.retpack(false)
    end

    return skynet.retpack(true)
end

function CMD.set_gm_time(data)
    gm_time = data
    skynet.retpack(true)
end

function dispatch_cmd(session,source,action,...)
    if not CMD[action] then
        errlog("unkown action", action)
    end

    CMD[action](...)
end

function start()
    skynet.start(function()
        skynet.dispatch("lua", function(session,source,action,...)
            dispatch_cmd(session,source,action,...)
        end)

        -- sharedata.query("global_configs")
        -- global_configs = setmetatable({},{
        --     __index = function(t,k)
        --         return sharedata.query("global_configs")[k]
        --     end
        -- })
        global_configs = setmetatable({},{
            __index = function(t,k) 
                --return sharedata.query("global_configs")[k]
                return sharedata.query(k)
            end
        })
    end)
end

_START(function()
    print('now this service is starting')
    init()
    start()
end)